﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using MyClever.Lib;

namespace MyCleverMissionEditor
{
    public partial class PackageSheetAccordion : Panel
    {
        public event EventHandler Refreshed;
        
        public List<PackageSheet> PackageSheets
        {
            get
            {
                List<PackageSheet> expanderHeaders = new List<PackageSheet>();
                foreach (PackageSheet expanderOn in Controls)
                {
                    expanderHeaders.Add(expanderOn);
                }
                return expanderHeaders;
            }
        }
        
        public PackageSheetAccordion()
        {
            InitializeComponent();
            this.UpdateStyles();
        }

        public void Add(PackageSheet packageSheet)
        {
            packageSheet.Toggled += new EventHandler(OnAccordionRefresh);
            packageSheet.Collapse();
            this.Controls.Add(packageSheet);

            this.ArrangeLayout();
        }

        /// <summary>
        /// This event is called, if any expander was expanded/collapsed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnAccordionRefresh(object sender, EventArgs e)
        {
            Debug.Assert(this.Refreshed != null);
            
            foreach (PackageSheet expanderOn in Controls)
            {
                if (expanderOn == ((PackageSheet)sender))
                {
                    continue;                
                }
                else
                {
                    expanderOn.Collapse();
                }
            }
            this.ArrangeLayout();

            this.Refreshed(this, null);
        }

        private void ArrangeLayout()
        {
            int height = this.Padding.Top;
            int width = 0;
            foreach (PackageSheet packageSheetOn in this.Controls)
            {
                packageSheetOn.Location = new Point(packageSheetOn.Location.X, height);

                height += packageSheetOn.Height - 2;
                width = packageSheetOn.Width;
            }
            this.Size = new Size(width, height + 10);            
        }
    }
}
